<?php
	function habilitation()
	{
		$hab = array(
			1,
			2
		);
		
		if (in_array(get_s('bo_user_role'), $hab))
			return true;
		return false;
	}
	
	function view($table, $id)
	{
		if (habilitation())
		{
			$retour = array();
			$champs = champs();
			$champaff = '';
			foreach ($champs as $champ)
				$champaff .= $champ['id'] . ', ';
			$champaff = substr($champaff, 0, -2);
			$gp_db = GP_db::getInstance(BDD_DSN, BDD_UTILISATEUR, BDD_MOT_DE_PASSE);
			$sql = "SELECT $champaff FROM $table WHERE id = $id";
			$tab = $gp_db -> fetchArray_one($sql);
			
			foreach ($tab as $cle => $valeur)
			{
				$type = $champs[$cle]["type"];
				if ($type == 'input')
					$retour[$champs[$cle]["Affichage"]] = sl($valeur);
				else if ($type == 'password')
					$retour[$champs[$cle]["Affichage"]] = '***';
				else if ($type == 'select')
				{
					$type_select = $champs[$cle]["typeselect"];
					
					if ($type_select == 'vocabulaire')
					{
						$vocabulaire = $champs[$cle]["data"];
						$tabvoc = explode(';', $vocabulaire);
						
						foreach ($tabvoc as $seg)
						{
							list($seg_c, $seg_v) = explode('%%', $seg, 2);
							if ($seg_c == $valeur)
							{
								$retour[$champs[$cle]["Affichage"]] = sl($seg_v);
								break;
							}
						}
					}
					
					else if ($type_select == 'sql')
					{
						$requete = $champs[$cle]["data"];
						$tagseg = $gp_db -> fetchRow($requete);
						foreach ($tagseg as $ligne)
						{
							if ($ligne[0] == $valeur)
							{
								$retour[$champs[$cle]["Affichage"]] = sl($ligne[1]);
								break;
							}
						}
					}
				}
			}
			
			return $retour;
		}
		return false;
	}
	
	function champs()
	{
		$retour = array();
		
		$retour['firstname']['id'] = 'firstname';
		$retour['firstname']['Affichage'] = 'Prénom';
		$retour['firstname']['max'] = 255;
		$retour['firstname']['type'] = 'input';
		$retour['firstname']['obligatoire'] = true;
		
		$retour['surname']['id'] = 'surname';
		$retour['surname']['Affichage'] = 'Nom';
		$retour['surname']['max'] = 255;
		$retour['surname']['type'] = 'input';
		$retour['surname']['obligatoire'] = true;
		
		$retour['address']['id'] = 'address';
		$retour['address']['Affichage'] = 'Adresse';
		$retour['address']['max'] = 255;
		$retour['address']['type'] = 'input';
		$retour['address']['obligatoire'] = true;
		
		$retour['address_2']['id'] = 'address_2';
		$retour['address_2']['Affichage'] = 'Adresse (suite)';
		$retour['address_2']['max'] = 255;
		$retour['address_2']['type'] = 'input';
		$retour['address_2']['obligatoire'] = false;
		
		$retour['zip']['id'] = 'zip';
		$retour['zip']['Affichage'] = 'Code postal';
		$retour['zip']['max'] = 255;
		$retour['zip']['type'] = 'input';
		$retour['zip']['obligatoire'] = true;
		
		$retour['city']['id'] = 'city';
		$retour['city']['Affichage'] = 'Ville';
		$retour['city']['max'] = 255;
		$retour['city']['type'] = 'input';
		$retour['city']['obligatoire'] = true;
		
		$retour['country']['id'] = 'country';
		$retour['country']['Affichage'] = 'Pays';
		$retour['country']['max'] = 255;
		$retour['country']['type'] = 'input';
		$retour['country']['obligatoire'] = true;
		
		$retour['email']['id'] = 'email';
		$retour['email']['Affichage'] = 'Courriel';
		$retour['email']['max'] = 255;
		$retour['email']['type'] = 'input';
		$retour['email']['obligatoire'] = true;
		
		$retour['birthdate']['id'] = 'birthdate';
		$retour['birthdate']['Affichage'] = 'Date de naissance';
		$retour['birthdate']['max'] = 11;
		$retour['birthdate']['type'] = 'date';
		$retour['birthdate']['obligatoire'] = true;

		return $retour;
	}
	
	
	$this -> setvar('titre', 'Afficher un inscrit');
	$this -> setvar('retour_url', rw('mailing_list'));
	$this -> setvar('habilitation', habilitation());
	$this -> setvar('tableau', view($this -> table, $this -> id));
?>